package com.ruoyi.project.tool.swagger.AppApi;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysAlarm;
import com.ruoyi.project.system.service.ISysAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 报警记录Controller
 *
 * @author jjp
 * @date 2020-03-30
 */
@RestController
@RequestMapping("/appapi/alarm")
public class AlarmController extends BaseController
{
    @Autowired
    private ISysAlarmService sysAlarmService;

    /**
     * 查询报警记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAlarm sysAlarm)
    {
        startPage();
        List<SysAlarm> list = sysAlarmService.selectSysAlarmList(sysAlarm);
        return getDataTable(list);
    }

//    /**
//     * 获取报警记录详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:alarm:query')")
//    @GetMapping(value = "/{alarmId}")
//    public AjaxResult getInfo(@PathVariable("alarmId") Long alarmId)
//    {
//        return AjaxResult.success(sysAlarmService.selectSysAlarmById(alarmId));
//    }

    /**
     * 新增报警记录
     */
    @Log(title = "报警记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAlarm sysAlarm){
        return toAjax(sysAlarmService.insertSysAlarm(sysAlarm));
    }



    /**
     * 修改报警记录
     */
    @Log(title = "报警记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAlarm sysAlarm){
        return toAjax(sysAlarmService.updateSysAlarm(sysAlarm));
    }

    /**
     * 删除报警记录
     */
    @Log(title = "报警记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{alarmIds}")
    public AjaxResult remove(@PathVariable Long[] alarmIds){
        return toAjax(sysAlarmService.deleteSysAlarmByIds(alarmIds));
    }


    @Log(title = "报警图片", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException{
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
